//
//  AddMember.m
//  PCS-IPHONE
//
//  Created by zhouChaoLin on 16/12/11.
//  Copyright © 2016年 Henry. All rights reserved.
//

#import "AddMember.h"

#define cellHeight  50

@interface AddMember ()<UIScrollViewDelegate>{

     BOOL  isSearch;
    NSMutableArray  *arrDataSearch;
}

@end

@implementation AddMember

@synthesize arrMainData,mainTableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self  initDataAndUI];
    
    
}


-(void)initDataAndUI{
    
    VIEW_CORNER(self.mainTableView, 4.0);
    NSDictionary  *dicJson=  [CMCCIOTIGUnity   getRequestJson:@{}];
    arrDataSearch=[[NSMutableArray alloc]  init];
    arrMainData=[NSMutableArray new];
    
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]};
    
    self.textSearch.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"关键字搜索" attributes:dic];
    
    [[NSNotificationCenter     defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:self.textSearch];
    
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:GetMemberList andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            NSArray  *arrData;
            if ([responseJson[@"data"]  isKindOfClass:[NSArray  class]]) {
                arrData=responseJson[@"data"];
            }
            else{
                arrData=[@[]  mutableCopy];
            }
            
            [arrData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                MemberList  *member=[[MemberList  alloc]  init];
                NSDictionary  *dicMember=obj;
                member.isSaleHost=[(NSString  *)dicMember[@"isSaleHost"]  nullClass];
                member.userAccount=[(NSString  *)dicMember[@"userAccount"]  nullClass];
                member.FEntryID=[STRING_FORMAT(dicMember[@"FEntryID"])  integerValue ]  ;
                member.post=[dicMember[@"post"]  nullClass];
                
                member.userID=[dicMember[@"userID"]  integerValue];
                member.userName=[(NSString  *)dicMember[@"userName"]  nullClass];
                ;
                [arrMainData addObject:member];
                
            }];
            
            [mainTableView reloadData];
            
            [self  resetFrame:arrMainData.count];
        }
        else{
           
            [self  resetFrame:0];
            
        }
        
        
        
    } andFailBlock:^(NSError *error) {
        [self  resetFrame:0];
        
    } ];
    
}



#pragma mark Tableview Deleget

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (isSearch) {
        return   arrDataSearch.count;
    }
    else{
        return   arrMainData.count;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdentifer=@"InventoryListCell";
    
    UITableViewCell   *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if(!cell){
        
        cell=[[UITableViewCell  alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        
    }
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    
    NSArray  *arrDataCurrent=isSearch?arrDataSearch:arrMainData;
    MemberList  *member=arrDataCurrent[indexPath.row];
    cell.textLabel.text=member.userName;
    
    UIImageView  *imageLine=[[UIImageView alloc]  initWithFrame:CGRectMake(0, cellHeight-1, tableView.widthV, 1)];
    imageLine.backgroundColor=[UIColor  lightGrayColor];
    imageLine.alpha=0.2;
    [cell.contentView  addSubview:imageLine];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight ;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray  *arrDataCurrent= isSearch?arrDataSearch:arrMainData;
    MemberList  *member=arrDataCurrent[indexPath.row];
    
    self.vertoryBlock(member);
    [self.navigationController  popViewControllerAnimated:YES];
    
}

- (IBAction)popBack:(id)sender {
    
    [self.navigationController  popViewControllerAnimated:YES];
     [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)resetFrame:(NSInteger )index{
    
    self.tableViewHeight.constant=index*cellHeight;
    
    if ( self.tableViewHeight.constant>SCREEN_HEIGHT-160) {
        
        self.tableViewHeight.constant=SCREEN_HEIGHT-160;
    }
    
}

#pragma mark  textField delegate

-(void)textDidChange:(NSNotification *)object{
    
    UITextField  *text= [object  object];
    NSString  *searchStr=text.text;
    if (searchStr.length) {
        isSearch=YES;
        arrDataSearch=[[NSMutableArray  alloc]  init];
        for (int i=0; i<arrMainData.count; i++) {
            MemberList  *member=arrMainData[i];
            NSString  *userName=member.userName;
            if ([userName rangeOfString:searchStr].length!=0) {
                [arrDataSearch addObject:member];
            }
            
        }
         [self  resetFrame:arrDataSearch.count];
            }
    else{
        isSearch=NO;
         [self  resetFrame:arrMainData.count];
    }
   
    [self.mainTableView reloadData];

    

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [self.view endEditing:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
